package com.example.demo.module;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @program: demo
 * @description: Teams SSO 验证
 * @author: ref.tian
 * @create: 2021-05-07 13:17
 **/
@RestController
@RequestMapping("api/v1")
@Slf4j
public class MicrosoftSso {

    @CrossOrigin
    @RequestMapping(value = "authtoken", method = RequestMethod.POST)
    public String authtoken(@RequestBody(required = true) AuthTokenDto authTokenDto) throws Exception {
        /*HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        headers.add("Accept","application/json");
        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
        postParameters.add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
        postParameters.add("client_id", "d0642ef0-74d2-48ea-9ba0-0dd69512c048");
        postParameters.add("client_secret", "~.U7E5-PLN2S_DAQdB6GxsHdMukz34ka0d");
//        postParameters.add("assertion", "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Im5PbzNaRHJPRFhFSzFqS1doWHNsSFJfS1hFZyJ9.eyJhdWQiOiJkMDY0MmVmMC03NGQyLTQ4ZWEtOWJhMC0wZGQ2OTUxMmMwNDgiLCJpc3MiOiJodHRwczovL2xvZ2luLm1pY3Jvc29mdG9ubGluZS5jb20vMDcyMDA5OGUtNTg2Ny00ZDdhLTk5ZjgtNzRhZGQ0MzFiYmM3L3YyLjAiLCJpYXQiOjE2MjAzNjQxMzIsIm5iZiI6MTYyMDM2NDEzMiwiZXhwIjoxNjIwMzcxNjMyLCJhaW8iOiJBVFFBeS84VEFBQUFFYmsyZzVtYmxnMGtIN0RSRXp2RUZNbjBqQzYxeEdtY2lXMWIvNGRudGoxcUFDUUV5R0JHVFZmWmR5dWVkQkJ6IiwiYXpwIjoiNWUzY2U2YzAtMmIxZi00Mjg1LThkNGItNzVlZTc4Nzg3MzQ2IiwiYXpwYWNyIjoiMCIsIm5hbWUiOiJyZWYgdGlhbiIsIm9pZCI6IjU4NTg2YjlhLWZkMTktNGJmZC04MzZjLWM4ZGI0MTAyMDYzNSIsInByZWZlcnJlZF91c2VybmFtZSI6InJlZkByZWZ0aWFuLm9ubWljcm9zb2Z0LmNvbSIsInJoIjoiMC5BWEFBamdrZ0IyZFllazJaLUhTdDFERzd4OERtUEY0Zks0VkNqVXQxN25oNGMwWndBSU0uIiwic2NwIjoiYWNjZXNzX2FzX3VzZXIgdGFiIiwic3ViIjoicmdfWkp2eGVBbHlTX1BiZFJkcGNQbDBJNmJxUE5HSzBhZHY1M2xMZlBIayIsInRpZCI6IjA3MjAwOThlLTU4NjctNGQ3YS05OWY4LTc0YWRkNDMxYmJjNyIsInV0aSI6Ii15Y25ZekliNkU2dTBESS0xQkpsQXciLCJ2ZXIiOiIyLjAifQ.YMVnHroSdEn29xxQENFNDcWRlXdhCbhA_cW99RzzSy00wdmHGv5DjyDIaD8-zFjK43NG48oPCWyiY8se7c8FPOHue3z_-6bImHRMnyvEnbeStHSNzYSYsKdjBFgymM8NFI4GLHE-KgT36USAol_5tjFiYKcqX67Sk3ZK5sAAYzn91D-wmu-O4-mrehQBnunyoxtOdmFRHZNNuHfz0S36wszZ_Pi4CX53uJDNMm994-Z3BQyctk4QePT-VfAFyd4GTAlWpf-1IIFoYhcrzUPutkbKw09Bn1EL_6Ces0KpErlSRooNz7_d72UMwPtKGoX1TJ8qtY5IglnaELhwBVsoiA");
        postParameters.add("assertion", authTokenDto.getAssertion());


        postParameters.add("scope", "https://graph.microsoft.com/User.Read");
        postParameters.add("requested_token_use", "on_behalf_of");

        HttpEntity<MultiValueMap<String, Object>> httpEntity =
                new HttpEntity<>(postParameters, headers);
        RestTemplate restTemplate = new RestTemplate();
        try{
            Map m = restTemplate.postForObject("https://login.microsoftonline.com/0720098e-5867-4d7a-99f8-74add431bbc7/oauth2/v2.0/token",
                    httpEntity, Map.class);
            return m.get("access_token").toString();
        }catch (Exception e){
            //todo
            log.error(e.getMessage(),e);
            throw new Exception(e.getMessage());
        }*/
        return "";
    }
}
